package cn.tedu.knows.search.service.impl;

import cn.tedu.knows.commons.model.Tag;
import cn.tedu.knows.commons.model.User;
import cn.tedu.knows.search.repository.QuestionRepository;
import cn.tedu.knows.search.service.IQuestionService;
import cn.tedu.knows.search.vo.Pages;
import cn.tedu.knows.search.vo.QuestionVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
public class QuestionServiceImpl implements IQuestionService {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private QuestionRepository questionRepository;

    @Override
    public void sync() {
        //先查总页数才能知道循环次数
        Integer pageSize = 8;
        String url = "http://faq-service/v1/" +
                "questions/page/count?pageSize={1}";
        Integer pages = restTemplate.getForObject(
                url, Integer.class, pageSize);
        //开始按照总页数循环
        for (int i = 1; i <= pages; i++) {
            //通过Ribbon获得当前页数据
            url = "http://faq-service/v1/questions/" +
                    "page?pageNum={1}&pageSize={2}";
            QuestionVo[] qs = restTemplate.getForObject(
                    url, QuestionVo[].class, i, pageSize);
            //将当前页数据增加到ES
            questionRepository.saveAll(
                    Arrays.asList(qs));
            log.debug("新增了第{}页数据", i);
        }
    }

    //根据用户名获得用户信息的方法
    public User getUser(String username) {
        String url = "http://sys-service/v1/auth/" +
                "user?username={1}";
        User user = restTemplate.getForObject(
                url, User.class, username);
        return user;
    }

    @Override
    public PageInfo<QuestionVo> search(String key, String username, Integer pageNum, Integer pageSize) {
        if (pageNum == null)
            pageNum = 1;
        if (pageSize == null)
            pageSize = 8;
        User user = getUser(username);
        //分页设置
        Pageable pageable = PageRequest.of(pageNum - 1,
                pageSize, Sort.Direction.DESC,
                "createtime");
        //执行查询
        Page<QuestionVo> page = questionRepository
                .queryAllByParams(key, key,
                        user.getId() + "", pageable);
        //开始为问题对象赋值标签List
        for(QuestionVo q: page){
            List<Tag> tags=tagNames2Tags(q.getTagNames());
            q.setTags(tags);
        }
        //转换+返回
        return Pages.pageInfo(page);

    }

    @Override
    public void saveQuestion(QuestionVo questionVo) {
        questionRepository.save(questionVo);
    }

    //要想显示每个问题对应的标签
    //思路和学生首页一样,要先获得包含所有标签的map

    private Map<String, Tag> name2TagMap =
            new ConcurrentHashMap<>();

    //编写一个方法通过Ribbon访问faq获得所有标签并向上面的map中赋值
    private Map<String, Tag> getName2TagMap() {
        if (name2TagMap.isEmpty()) {
            String url = "http://faq-service/v1/tags";
            Tag[] tags = restTemplate.getForObject(
                    url, Tag[].class);
            for (Tag t : tags) {
                name2TagMap.put(t.getName(), t);
            }
        }
        return name2TagMap;
    }


    private List<Tag> tagNames2Tags(String tagNames) {
        //tagNames:"java基础,javaSE,面试题"
        String[] names = tagNames.split(",");
        //names:{"java基础","javaSE","面试题"}
        List<Tag> tags = new ArrayList<>();
        Map<String, Tag> name2TagMap =getName2TagMap();
        for (String name : names) {
            tags.add(name2TagMap.get(name));
        }
        //千万别忘了返回
        return tags;
    }


}
